package com.loong.android.http.config;

import android.content.Context;

import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;

/**
 * HTTP请求配置
 */
public class HttpConfig {

    private HttpHeaders httpHeaders;
    private HttpParams httpParams;

    private final Context context;
    private boolean debugEnable = false;
    private int logType = LogType.BODY;
    private int colorType = ColorType.INFO;
    private int cookieMode = CookieMode.MEMORY;
    private int cacheMode = CacheMode.NO_CACHE;
    //-1表示永不过期
    private int cacheTime = -1;
    private int retryCount = 3;
    private int readTimeout = 60000;
    private int writeTimeout = 60000;
    private int connectTimeout = 60000;

    public HttpConfig(Context context) {
        this.context = context.getApplicationContext();
    }

    public Context getContext() {
        return context;
    }

    /**
     * 设置是否开启调试
     * @param debugEnable 是否开启
     */
    public HttpConfig setDebugEnable(boolean debugEnable) {
        this.debugEnable = debugEnable;
        return this;
    }

    /**
     * 取是否开启调试日志
     */
    public boolean isDebugEnable() {
        return debugEnable;
    }

    /**
     * 设置调试日志类型
     * @param logType 日志类型值，参考LogType内部类
     */
    public HttpConfig setDebugLogType(int logType) {
        this.logType = logType;
        return this;
    }

    /**
     * 取调试日志类型
     */
    public int getDebugLogType() {
        return logType;
    }

    /**
     * 设置调试日志颜色类型
     * @param colorType 颜色类型，参考ColorType内部类
     */
    public HttpConfig setDebugLogColorType(int colorType) {
        this.colorType = colorType;
        return this;
    }

    /**
     * 取调试日志颜色类型
     */
    public int getDebugLogColorType() {
        return colorType;
    }

    /**
     * 设置cookie保持模式
     *
     * @param cookieMode 模式，参考CookieMode内部类
     */
    public HttpConfig setCookieMode(int cookieMode) {
        this.cookieMode = cookieMode;
        return this;
    }

    /**
     * 取cookie保持模式
     */
    public int getCookieMode() {
        return cookieMode;
    }

    /**
     * 获取请求的读超时
     */
    public int getReadTimeout() {
        return readTimeout;
    }

    /**
     * 设置请求的读超时，默认为60s
     * @param readTimeout 超时时间，单位为毫秒
     */
    public HttpConfig setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
        return this;
    }

    /**
     * 获取请求的写超时时间
     */
    public int getWriteTimeout() {
        return writeTimeout;
    }

    /**
     * 设置请求的写超时，默认为60s
     * @param writeTimeout 超时时间，单位为毫秒
     */
    public HttpConfig setWriteTimeout(int writeTimeout) {
        this.writeTimeout = writeTimeout;
        return this;
    }

    /**
     * 获取请求的连接超时
     */
    public int getConnectTimeout() {
        return connectTimeout;
    }

    /**
     * 设置请求的连接超时，默认为60s
     * @param connectTimeout 超时时间，单位为毫秒
     */
    public HttpConfig setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }

    /**
     * 设置全局公共的头数据配置
     * @param httpHeaders 头数据配置
     */
    public HttpConfig setHttpHeaders(HttpHeaders httpHeaders) {
        this.httpHeaders = httpHeaders;
        return this;
    }

    /**
     * 取全局公共的头数据配置
     */
    public HttpHeaders getHttpHeaders() {
        return httpHeaders;
    }

    /**
     * 设置全局公共的参数配置
     *
     * @param httpParams 参数配置
     */
    public HttpConfig setHttpParams(HttpParams httpParams) {
        this.httpParams = httpParams;
        return this;
    }

    /**
     * 获取全局公共的参数配置
     */
    public HttpParams getHttpParams() {
        return httpParams;
    }

    /**
     * 设置全局统一缓存模式
     * @param cacheMode 缓存模式，参考内部类CacheMode
     */
    public HttpConfig setCacheMode(int cacheMode) {
        this.cacheMode = cacheMode;
        return this;
    }

    /**
     * 取全局统一缓存模式
     */
    public int getCacheMode() {
        return cacheMode;
    }

    /**
     * 取全局统一的缓存时间
     */
    public int getCacheTime() {
        return cacheTime;
    }

    /**
     * 设置全局统一的缓存时间
     *
     * @param cacheTime -1表示永不过期，默认为-1
     */
    public HttpConfig setCacheTime(int cacheTime) {
        this.cacheTime = cacheTime;
        return this;
    }

    /**
     * 取请求失败时重试的次数
     */
    public int getRetryCount() {
        return retryCount;
    }

    /**
     * 设置请求失败时，重试的次数
     *
     * @param retryCount 0表示不重试，默认为3次
     */
    public HttpConfig setRetryCount(int retryCount) {
        this.retryCount = retryCount;
        return this;
    }

    /**
     * 日志类型
     */
    public static class LogType {
        //不打印log
        public final static int NONE = 0;
        //只打印 请求首行 和 响应首行
        public final static int BASIC = 1;
        //打印请求和响应的所有 Header
        public final static int HEADERS = 2;
        //所有数据全部打印(默认)
        public final static int BODY = 3;
    }

    /**
     * 颜色类型
     */
    public static class ColorType {
        public final static int INFO = 0;
        public final static int CONFIG = 1;
        public final static int ALL = 2;
        public final static int OFF = 3;
        public final static int FINE = 4;
        public final static int FINER = 5;
        public final static int FINEST = 6;
        public final static int SEVERE = 7;
        public final static int WARNING = 8;
    }

    /**
     * cookie模式
     */
    public static class CookieMode {
        //使用SharePreference保持cookie，如果cookie不过期，则一直有效
        public final static int SP = 0;
        //使用数据库保持cookie，如果cookie不过期，则一直有效
        public final static int DB = 1;
        //使用内存保持cookie，应用退出后，cookie消失
        public final static int MEMORY = 2;
    }

    /**
     * 缓存模式
     */
    public static class CacheMode {
        //按照HTTP协议的默认缓存规则，例如有304响应头时缓存
        public final static int DEFAULT = 0;
        //不使用缓存
        public final static int NO_CACHE = 1;
        //请求网络失败后，读取缓存
        public final static int REQUEST_FAILED_READ_CACHE = 2;
        //如果缓存不存在才请求网络，否则使用缓存
        public final static int IF_NONE_CACHE_REQUEST = 3;
        //先使用缓存，不管是否存在，仍然请求网络
        public final static int FIRST_CACHE_THEN_REQUEST = 4;
    }

}
